package cn.calm.swing.osgi.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.SwingUtilities;

public class SwingHelper {
	static Toolkit kit = Toolkit.getDefaultToolkit();
	static Dimension screenSize = kit.getScreenSize();
	
	public static void swingInvoke(Runnable runner) {
		if (SwingUtilities.isEventDispatchThread()) {
            runner.run();
            return;
        }
        try {
            SwingUtilities.invokeAndWait(runner);
        } catch (Exception ex) {
            ex.printStackTrace();
        }		
	}

	public static void center(Component component){
		int width = (int) screenSize.getWidth();
		int height = (int) screenSize.getHeight();
		int w = component.getWidth();
		int h = component.getHeight();
		component.setLocation( (width - w) / 2, (height - h) / 2);
	}

}
